package com.miage.kangourou.server.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.miage.kangourou.client.beans.Matiere;

public class MatiereDao extends GenericDao<Matiere> {

	public MatiereDao(Connection connect) {
		super(connect);
	}

	public boolean alreadyExist(Matiere matiere) throws SQLException {
		boolean exist = false;

		if ((matiere instanceof Matiere) && !matiere.equals(null)) {

			ResultSet result = connect.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY).executeQuery(
					"SELECT * FROM MATIERE WHERE ID_MATIERE = "
							+ matiere.getId_matiere());

			if (result.next()) {
				exist = true;
			}

			result.close();
		}

		return exist;
	}

	public void create(Matiere matiere) throws SQLException {
		if ((matiere instanceof Matiere) && !matiere.equals(null)) {

			connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY).executeQuery(
					"INSERT INTO MATIERE (ID_UE, INTITULE) VALUES ("
							+ matiere.getId_ue() + ", \""
							+ matiere.getIntitule() + "\")");
		}
	}

	public boolean delete(int idMatiere) {
		boolean deleted = true;

		try {
			connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY).executeQuery(
					"DELETE FROM MATIERE WHERE ID_MATIERE = " + idMatiere);
		} catch (SQLException e) {
			e.printStackTrace();
			deleted = false;
		}

		return deleted;
	}

	public List<Matiere> findAll() throws SQLException {
		ResultSet result = connect.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
				.executeQuery("SELECT * FROM MATIERE");

		List<Matiere> all = new ArrayList<Matiere>();
		while (result.next()) {
			all.add(new Matiere(result.getInt("ID_MATIERE"), result
					.getInt("ID_UE"), result.getString("INTITULE")));
		}

		result.close();

		return all;
	}

	public Matiere findById(int id) throws SQLException {
		ResultSet result = connect.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
				.executeQuery("SELECT * FROM MATIERE WHERE ID_MATIERE = " + id);

		Matiere matiere;

		if (result.next()) {
			matiere = new Matiere(result.getInt("ID_MATIERE"), result
					.getInt("ID_UE"), result.getString("INTITULE"));
		} else {
			matiere = new Matiere();
		}

		result.close();

		return matiere;
	}

	public List<Matiere> findByIdUe(int ue) throws SQLException {
		ResultSet result = connect.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
				.executeQuery("SELECT * FROM MATIERE WHERE ID_UE = " + ue);

		List<Matiere> all = new ArrayList<Matiere>();
		while (result.next()) {
			all.add(new Matiere(result.getInt("ID_MATIERE"), result
					.getInt("ID_UE"), result.getString("INTITULE")));
		}

		result.close();

		return all;
	}

}
